package cn.edu.hitwh.kanlema.service.impl;

import cn.edu.hitwh.kanlema.constant.RedisConstants;
import cn.edu.hitwh.kanlema.exception.ApiException;
import cn.edu.hitwh.kanlema.service.ICinemaService;
import cn.edu.hitwh.kanlema.service.IVoucherOrderService;
import cn.edu.hitwh.kanlema.service.IVoucherService;
import cn.edu.hitwh.kanlema.util.RedisCache;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.hitwh.kanlema.dto.Result;
import cn.edu.hitwh.kanlema.entity.Voucher;
import cn.edu.hitwh.kanlema.mapper.VoucherMapper;
import cn.edu.hitwh.kanlema.entity.SeckillVoucher;
import cn.edu.hitwh.kanlema.service.ISeckillVoucherService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author vvcoder
 * @since 2025-8-18
 */
@Service
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements IVoucherService {

    @Resource
    private ISeckillVoucherService seckillVoucherService;

    @Resource
    private ICinemaService cinemaService;

    @Resource
    private IVoucherOrderService voucherOrderService;

    @Resource
    private RedisCache redisCache;

    @Override
    public Result queryVoucherOfCinema(Long cinemaId) {
        if (!cinemaService.mightExist(cinemaId)) {
            return Result.fail("影院不存在");
        }

        // 查询优惠券信息
        String vouchersJson = redisCache.getValueOrSet(
                RedisConstants.VOUCHER_OF_CINEMA_KEY + cinemaId,
                () -> JSONUtil.toJsonStr(getBaseMapper().queryVoucherOfCinema(cinemaId)),
                RedisConstants.CACHE_VOUCHER_OF_CINEMA_TTL,
                TimeUnit.MINUTES
        );
        // 返回结果
        return Result.ok(JSONUtil.toList(vouchersJson, Voucher.class));
    }

    @Override
    @Transactional
    public void addSeckillVoucher(Voucher voucher) {
        // 不能在秒杀开始一小时内前添加优惠券
        if (voucher.getBeginTime().isBefore(LocalDateTime.now().minusHours(1))) {
            throw new ApiException("无法在秒杀开始一小时前添加优惠券");
        }
        // 持续时间不能小于3分钟
        if (voucher.getEndTime().isBefore(voucher.getBeginTime().plusMinutes(3))) {
            throw new ApiException("持续时间不能小于3分钟");
        }

        // 保存优惠券
        save(voucher);
        // 保存秒杀信息
        SeckillVoucher seckillVoucher = new SeckillVoucher();
        seckillVoucher.setVoucherId(voucher.getId());
        seckillVoucher.setStock(voucher.getStock());
        seckillVoucher.setBeginTime(voucher.getBeginTime());
        seckillVoucher.setEndTime(voucher.getEndTime());
        seckillVoucherService.save(seckillVoucher);
        // 如果当天开始秒杀，需要提前预热优惠券库存
        if (voucher.getBeginTime().toLocalDate().equals(LocalDate.now()) ) {
            voucherOrderService.cacheVoucher(seckillVoucher);
        }
    }
}
